from backend.backend import Backend
from workflow.task import Task
from workflow.result import Result 
from workflow.flowData import Status
from _ast import Pass


class PaymentBackend(Backend):

    def __init__(self): 
        # Specify the workflow name:       
        Backend.__init__(self, "CantonCandy")  # !!! be sure you update the workflow name
        
    def wire(self):
        '''
        The wire method is where we tell MWP which tasks to keep track of.
        To register a task, you will need to add a line of code that looks
        like this:
        self.register_result_listener("RoleName", "TaskName", self.method_name)
        Where: RoleName is the person who did the task
               TaskName is the name of the task they did
               self.method_name refers to a method in this class which should respond to
                                the task being completed.
        '''
        self.register_result_listener("Cashier", "OrderAccept", self.candy_order_taken)
        self.register_result_listener("Cashier", "ProcessPayment", self.candy_processed_payment)
        self.register_result_listener("Cashier", "ProcessCreditCard", self.processed_credit_card)
        #self.register_result_listener("Hershey", "PrepareCandy", self.candy_prepared)
        # !!! add a line of code here to register the step where a drink order is taken.
        # !!! add a line of code here to register the step where a drink is prepared.


    def candy_order_taken(self, results):
        for result in results:  # repeat the following actions for each result
            # !!! Fix the line below...
            if result.data['paymenttype'] == "Cash":
                task = Task.construct_from_result(result, "Cashier", "ProcessPayment")
                self.workflow.add(task) # add the new task to the workflow
                self.workflow.update_status(result, Status.COMPLETE)
            else:
                task = Task.construct_from_result(result, "Cashier", "ProcessCreditCard")
                self.workflow.add(task) # add the new task to the workflow
                self.workflow.update_status(result, Status.COMPLETE)


        

    def processed_credit_card (self, results):
        for result in results:  # repeat the following actions for each result
            task = Task.construct_from_result(result, "Cashier", "ProcessPayment")
            self.workflow.add(task) # add the new task to the workflow
            self.workflow.update_status(result, Status.COMPLETE)
        
    def candy_processed_payment(self, results):
        for result in results:  # repeat the following actions for each result
            # !!! Fix the line below...
            self.workflow.update_status(result, Status.COMPLETE)
    
if __name__ == '__main__':
    backend = PaymentBackend()        